package com.slipper.collect.support;

import com.slipper.collect.IDataConvert;
import com.slipper.util.DateUtil;
import com.slipper.util.Tool;

import java.time.format.DateTimeFormatter;

public class DateFormatDataConvert implements IDataConvert {
    @Override
    public Object convert(String param, Object data) {
        if (data == null) return null;

        String format = Tool.isBlank(param) ? "yyyy-MM-dd HH:mm:ss" : param;
        return switch (data) {
            case java.sql.Time date -> DateUtil.format(date, format);
            case java.sql.Date date -> DateUtil.format(date, format);
            case java.sql.Timestamp date -> DateUtil.format(date, format);
            case java.util.Date date -> DateUtil.format(date, format);
            case java.time.LocalDate date -> date.format(DateTimeFormatter.ofPattern(format));
            case java.time.LocalTime date -> date.format(DateTimeFormatter.ofPattern(format));
            case java.time.LocalDateTime date -> date.format(DateTimeFormatter.ofPattern(format));
            default -> null;
        };
    }
}
